package net.lcheng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.lcheng.commons.utils.PageList;
import net.lcheng.dao.PProductMapper;
import net.lcheng.dao.WxShopCategoryMapper;
import net.lcheng.dao.WxShopMapper;
import net.lcheng.model.PProduct;
import net.lcheng.model.PProductExample;
import net.lcheng.model.WxShopCategory;
import net.lcheng.service.ProductService;
import net.lcheng.vo.ProductConditionsSelectModel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by LV on 2016/7/1.
 */
@Service("productService")
public class ProductServiceImpl implements ProductService {
    @Resource
    private PProductMapper productMapper;
    @Resource
    private WxShopCategoryMapper shopCategoryMapper;

    /**
     * 发布商品
     * @param product
     * @return
     */
    @Override
    public int addProduct(PProduct product) {
        return productMapper.insert(product);
    }

    /**
     * 发布商品（重构7.21）
     * @param product 发布的商品对象
     * @return int
     */
    @Transactional
    @Override
    public int publishProduct(PProduct product) {
        //发布商品包含两个动作，将商品添加到p_product表，然后将wx_shop_category表对应的分类商品数+1
        if (product!=null) {
            int status=productMapper.insertSelective(product);
            //商品添加成功，就将对应的店铺对应的分类的产品数+1
            if (status>0) {
                WxShopCategory shopCategory = shopCategoryMapper.selectByPrimaryKey(product.getWxShopCategoryId());
                shopCategory.setProductCount(shopCategory.getProductCount()+1);
                int status1=shopCategoryMapper.updateByPrimaryKeySelective(shopCategory);
                if (status1>0)
                    return 1;
                else  return 0;
            }
            return 0;
        }
        return 0;
    }

    /**
     * 查询所有商品
     * @param shopId
     * @param pageSize
     * @param pageIndex
     * @return
     */
    @Override
    public PageList<PProduct> getProductListAll(int shopId, int pageSize, int pageIndex) {
        PageHelper.startPage(pageIndex,pageSize);

        PProductExample example=new PProductExample();
        example.createCriteria().andWxShopShopIdEqualTo(shopId);

        example.setOrderByClause("order_num DESC");//按order_num降序排列

        List<PProduct> list =productMapper.selectByExample(example);

        PageInfo<PProduct> pageInfo=new PageInfo<>(list);
        PageList<PProduct> pageList=new PageList<>();
        pageList.list=pageInfo.getList();
        pageList.pageIndex=pageInfo.getPageNum();
        pageList.pageSize=pageInfo.getPageSize();
        pageList.totalPage=pageInfo.getPages();
        pageList.totalCount=pageInfo.getTotal();

        return pageList;
    }

    /**
     * 查询所有商品
     * @param shopId
     * @return
     */
    @Override
    public List<PProduct> getProductListAll(int shopId) {
        PProductExample example=new PProductExample();
        example.createCriteria().andWxShopShopIdEqualTo(shopId);

        example.setOrderByClause("order_num DESC");//按order_num降序排列

        return productMapper.selectByExample(example);
    }

    /**
     * 根据销售状态查找产品列表
     * @param status
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public PageList<PProduct> getProductListByStatus(int shopId,int status,  int pageSize,int pageIndex) {
        PageHelper.startPage(pageIndex,pageSize);

        PProductExample example=new PProductExample();
        example.createCriteria().andStatusEqualTo(status)
                                .andWxShopShopIdEqualTo(shopId);
        example.setOrderByClause("order_num DESC");
        List<PProduct> list =productMapper.selectByExample(example);

        PageInfo<PProduct> pageInfo=new PageInfo<>(list);
        PageList<PProduct> pageList=new PageList<>();
        pageList.list=pageInfo.getList();
        pageList.pageIndex=pageInfo.getPageNum();
        pageList.pageSize=pageInfo.getPageSize();
        pageList.totalPage=pageInfo.getPages();
        pageList.totalCount=pageInfo.getTotal();

        return pageList;
    }

    /**
     * 根据销售状态查找产品列表
     * @param shopId
     * @param status
     * @return
     */
    @Override
    public List<PProduct> getProductListByStatus(int shopId, int status) {
        PProductExample example=new PProductExample();
        example.createCriteria().andStatusEqualTo(status)
                .andWxShopShopIdEqualTo(shopId);
        example.setOrderByClause("order_num DESC");
         return productMapper.selectByExample(example);
    }

    /**
     * 改变销售状态
     * @param productId
     * @param status
     * @return
     */
    @Override
    public int updateProductStatus(String productId, int status) {
        PProduct product=new PProduct();
        product.setProductId(productId);
        product.setStatus(status);
        return productMapper.updateByPrimaryKeySelective(product);
    }

    /**
     * 改变店铺推荐的状态
     * @param productId
     * @param status
     * @return
     */
    @Override
    public int updateProductRecommendStatus(String productId, int status) {
        PProduct product=new PProduct();
        product.setProductId(productId);
        product.setIsShopRecommend(status);
        return productMapper.updateByPrimaryKeySelective(product);
    }

    /**
     * 根据主键查询产品
     * @param productId
     * @return
     */
    @Override
    public PProduct getProductByPrimaryKey(String productId) {
        return productMapper.selectByPrimaryKey(productId);
    }

    /**
     * 更新产品
     * @param product
     * @return
     */
    @Override
    public int updateProductByPrimaryKey(PProduct product) {
        return productMapper.updateByPrimaryKeySelective(product);
    }

    /**
     * 多条件查询商品
     * @param shopId
     * @param conditions
     * @param pageSize
     * @param pageIndex
     * @return
     */
    @Override
    public PageList<PProduct> getProductByConditions(int shopId, ProductConditionsSelectModel conditions, int pageSize, int pageIndex) {
        PageHelper.startPage(pageIndex,pageSize);

        PProductExample example=new PProductExample();
        PProductExample.Criteria criteria=example.createCriteria();

        criteria.andWxShopShopIdEqualTo(shopId);

        if (conditions.getpName()!=null&&!conditions.getpName().equals(""))
            criteria.andProductNameEqualTo(conditions.getpName());
        if (conditions.getpCode()!=null&&!conditions.getpCode().equals(""))
            criteria.andProductCodeEqualTo(conditions.getpCode());
        if (conditions.getpCategoryId()!=null&&!conditions.getpCategoryId().equals(""))
            criteria.andWxShopCategoryIdEqualTo(conditions.getpCategoryId());

        example.setOrderByClause("order_num DESC");//按order_num降序排列

        List<PProduct> list=productMapper.selectByExample(example);

        PageInfo<PProduct> pageInfo=new PageInfo<>(list);
        PageList<PProduct> pageList=new PageList<>();
        pageList.list=pageInfo.getList();
        pageList.pageIndex=pageInfo.getPageNum();
        pageList.pageSize=pageInfo.getPageSize();
        pageList.totalPage=pageInfo.getPages();
        pageList.totalCount=pageInfo.getTotal();
        return pageList;
    }

    /**
     * 变更产品的店铺分类
     * @param productId 目标产品Id
     * @param categoryId 需要变更的分类Id
     * @return int
     */
    @Transactional
    @Override
    public int changeProductShopCategory(String productId, String categoryId) {
        //首先给产品添加新的分类，然后将原来分类的产品数减1（如果产品在发布时就没有分类就不处理），新加的分类产品加1
        //查询该产品信息
        PProduct product=productMapper.selectByPrimaryKey(productId);
        if (product!=null) {
            //检查该商品是否添加了分类
            String shopCategoryId = product.getWxShopCategoryId();
            //如果添加了分类，将原来产品数减1
            if (shopCategoryId != null && !shopCategoryId.equals("")) {
                WxShopCategory shopCategoryPre = shopCategoryMapper.selectByPrimaryKey(shopCategoryId);
                //如果之前的分类不存在，不执行商品减1
                if (shopCategoryPre!=null) {
                    shopCategoryPre.setProductCount(shopCategoryPre.getProductCount() - 1);
                    shopCategoryMapper.updateByPrimaryKeySelective(shopCategoryPre);
                }
                //更改新商品
                    WxShopCategory shopCategoryNew = shopCategoryMapper.selectByPrimaryKey(categoryId);
                    shopCategoryNew.setProductCount(shopCategoryNew.getProductCount() + 1);
                    int status1 = shopCategoryMapper.updateByPrimaryKeySelective(shopCategoryNew);
                    //更新产品分类
                    if (status1 > 0) {
                        product.setWxShopCategoryId(categoryId);
                        int status2 = productMapper.updateByPrimaryKeySelective(product);

                        if (status2 > 0)
                            return 1;
                        else return 0;
                    }
                    return 0;
            } else {
                WxShopCategory shopCategoryNew = shopCategoryMapper.selectByPrimaryKey(categoryId);
                shopCategoryNew.setProductCount(shopCategoryNew.getProductCount() + 1);
                int status = shopCategoryMapper.updateByPrimaryKeySelective(shopCategoryNew);
                //更新产品分类
                if (status > 0) {
                    product.setWxShopCategoryId(categoryId);
                    int status1 = productMapper.updateByPrimaryKeySelective(product);

                    if (status1 > 0)
                        return 1;
                    else return 0;
                }
                return 0;
            }
        }
        return 0;
    }
}
